/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.db.mysql;

import org.ajiaojr.enigmagm.client.data.Purchase;
import org.ajiaojr.enigmagm.client.service.DataAccessException;
import org.ajiaojr.enigmagm.db.ConnectionManager;
import org.ajiaojr.enigmagm.db.PurchaseDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ajiaojr
 */
public class MySQLPurchaseDAOImpl implements PurchaseDAO {
  protected static final Log LOG =
      LogFactory.getLog(MySQLPurchaseDAOImpl.class);

  public MySQLPurchaseDAOImpl() {
  }

  public List findByCharacterID(int characterID, String sortAttribute,
      boolean ascending, int firstResult, int maxResults)
      throws DataAccessException {
    Connection c = null;

    try {
      c = ConnectionManager.getInstance().getConnection();

      List result = new ArrayList();

      Statement s = c.createStatement();

      String sql =
          "SELECT character_name, item_id, item_name, event_id, raid_boss_name, event_time, price FROM purchase_list WHERE character_id = \""
              + characterID
              + "\" ORDER BY "
              + sortAttribute
              + " "
              + (ascending ? "ASC" : "DESC")
              + " LIMIT "
              + firstResult
              + " , "
              + maxResults;

      ResultSet r = s.executeQuery(sql);

      if (LOG.isDebugEnabled()) {
        LOG.debug("Finding purchases according to character name using query: "
            + sql);
      }

      while (r.next()) {
        result.add(new Purchase(characterID, r.getString(1), r.getInt(2), r
            .getString(3), r.getInt(4), r.getString(5), r.getTimestamp(6)
            .getTime(), r.getDouble(7)));
      }

      r.close();
      s.close();

      return result;
    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * @param eventID
   * @param sortAttribute
   * @param ascending
   * @param firstResult
   * @param maxResults
   * @return
   * @throws DataAccessException
   */
  public List findByEventID(int eventID, String sortAttribute,
      boolean ascending, int firstResult, int maxResults)
      throws DataAccessException {
    Connection c = null;

    try {
      c = ConnectionManager.getInstance().getConnection();

      List result = new ArrayList();

      Statement s = c.createStatement();

      String sql =
          "SELECT character_id, character_name, item_id, item_name, event_time, price FROM purchase_list WHERE event_id = "
              + eventID
              + " ORDER BY "
              + sortAttribute
              + " "
              + (ascending ? "ASC" : "DESC")
              + " LIMIT "
              + firstResult
              + " , "
              + maxResults;

      if (LOG.isDebugEnabled()) {
        LOG
            .debug("Finding purchases according to event id using query: "
                + sql);
      }

      ResultSet r = s.executeQuery(sql);

      while (r.next()) {
        result.add(new Purchase(r.getInt(1), r.getString(2), r.getInt(3), r
            .getString(4), eventID, null, r.getTimestamp(5).getTime(), r
            .getDouble(6)));
      }

      r.close();
      s.close();

      return result;
    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  public List findByItemID(int itemID, int firstResult, int maxResults)
      throws DataAccessException {
    // TODO Auto-generated method stub
    return null;
  }
}
